<template>
<div :style='{"width":"100%","padding":"30px 7% 40px","margin":"10px auto","position":"relative","background":"none","display":"block"}'>
    <el-form
      class="add-update-preview"
      ref="ruleForm"
      :model="ruleForm"
      :rules="rules"
      label-width="150px"
    >
          <el-form-item :style='{"padding":"10px","margin":"0 0 10px 0","background":"linear-gradient(180deg, rgba(255,255,255,1) 0%, rgba(178,213,232,1) 100%)"}' label="订单编号" prop="dingdanbianhao">
              <el-input v-model="ruleForm.dingdanbianhao" placeholder="订单编号" readonly></el-input>
          </el-form-item>
          <el-form-item :style='{"padding":"10px","margin":"0 0 10px 0","background":"linear-gradient(180deg, rgba(255,255,255,1) 0%, rgba(178,213,232,1) 100%)"}' label="商品名称" prop="shangpinmingcheng">
            <el-input v-model="ruleForm.shangpinmingcheng" 
                placeholder="商品名称" clearable ></el-input>
          </el-form-item>
          <el-form-item :style='{"padding":"10px","margin":"0 0 10px 0","background":"linear-gradient(180deg, rgba(255,255,255,1) 0%, rgba(178,213,232,1) 100%)"}' label="商品价格" prop="shangpinjiage">
            <el-input v-model="ruleForm.shangpinjiage" 
                placeholder="商品价格" clearable ></el-input>
          </el-form-item>
          <el-form-item :style='{"padding":"10px","margin":"0 0 10px 0","background":"linear-gradient(180deg, rgba(255,255,255,1) 0%, rgba(178,213,232,1) 100%)"}' label="商品重量" prop="shangpinzhongliang">
            <el-input v-model="ruleForm.shangpinzhongliang" 
                placeholder="商品重量" clearable ></el-input>
          </el-form-item>
          <el-form-item :style='{"padding":"10px","margin":"0 0 10px 0","background":"linear-gradient(180deg, rgba(255,255,255,1) 0%, rgba(178,213,232,1) 100%)"}' label="超重费用" prop="chaozhongfeiyong">
            <el-input v-model="ruleForm.chaozhongfeiyong" 
                placeholder="超重费用" clearable ></el-input>
          </el-form-item>
          <el-form-item :style='{"padding":"10px","margin":"0 0 10px 0","background":"linear-gradient(180deg, rgba(255,255,255,1) 0%, rgba(178,213,232,1) 100%)"}'  label="定金比例/%" prop="dingjinbili">
            <el-select v-model="ruleForm.dingjinbili" placeholder="请选择定金比例/%"  >
              <el-option
                  v-for="(item,index) in dingjinbiliOptions"
                  :key="index"
                  :label="item"
                  :value="item">
              </el-option>
            </el-select>
          </el-form-item>
          <el-form-item :style='{"padding":"10px","margin":"0 0 10px 0","background":"linear-gradient(180deg, rgba(255,255,255,1) 0%, rgba(178,213,232,1) 100%)"}' label="定金" prop="dingjin">
              <el-input v-model="dingjin" placeholder="定金" readonly></el-input>
          </el-form-item>
          <el-form-item :style='{"padding":"10px","margin":"0 0 10px 0","background":"linear-gradient(180deg, rgba(255,255,255,1) 0%, rgba(178,213,232,1) 100%)"}' label="收货地址" prop="shouhuodizhi">
            <el-input v-model="ruleForm.shouhuodizhi" 
                placeholder="收货地址" clearable ></el-input>
          </el-form-item>
          <el-form-item :style='{"padding":"10px","margin":"0 0 10px 0","background":"linear-gradient(180deg, rgba(255,255,255,1) 0%, rgba(178,213,232,1) 100%)"}' label="购物地址" prop="gouwudizhi">
            <el-input v-model="ruleForm.gouwudizhi" 
                placeholder="购物地址" clearable ></el-input>
          </el-form-item>
          <el-form-item :style='{"padding":"10px","margin":"0 0 10px 0","background":"linear-gradient(180deg, rgba(255,255,255,1) 0%, rgba(178,213,232,1) 100%)"}' label="购物说明" prop="gouwushuoming">
            <el-input v-model="ruleForm.gouwushuoming" 
                placeholder="购物说明" clearable ></el-input>
          </el-form-item>
          <el-form-item :style='{"padding":"10px","margin":"0 0 10px 0","background":"linear-gradient(180deg, rgba(255,255,255,1) 0%, rgba(178,213,232,1) 100%)"}' label="最晚完成时间" prop="zuiwanwanchengshijian">
            <el-input v-model="ruleForm.zuiwanwanchengshijian" 
                placeholder="最晚完成时间" clearable ></el-input>
          </el-form-item>
          <el-form-item :style='{"padding":"10px","margin":"0 0 10px 0","background":"linear-gradient(180deg, rgba(255,255,255,1) 0%, rgba(178,213,232,1) 100%)"}' label="订单时间" prop="dingdanshijian" >
              <el-date-picker
                  value-format="yyyy-MM-dd HH:mm:ss"
                  v-model="ruleForm.dingdanshijian" 
                  type="datetime"
                  placeholder="订单时间">
              </el-date-picker>
          </el-form-item>
          <el-form-item :style='{"padding":"10px","margin":"0 0 10px 0","background":"linear-gradient(180deg, rgba(255,255,255,1) 0%, rgba(178,213,232,1) 100%)"}' label="发布者" prop="xuehao">
            <el-input v-model="ruleForm.xuehao" 
                placeholder="发布者" clearable ></el-input>
          </el-form-item>
          <el-form-item :style='{"padding":"10px","margin":"0 0 10px 0","background":"linear-gradient(180deg, rgba(255,255,255,1) 0%, rgba(178,213,232,1) 100%)"}' label="代购者" prop="daigouzhanghao">
            <el-input v-model="ruleForm.daigouzhanghao" 
                placeholder="代购者" clearable ></el-input>
          </el-form-item>

      <el-form-item :style='{"padding":"0","margin":"20px 0 0","alignItems":"center","justifyContent":"center","display":"flex"}'>
        <el-button :style='{"border":"2px solid #2886bc","cursor":"pointer","padding":"0 35px","boxShadow":"inset 2px 0px 12px 0px #c2ddeb","margin":"0 10px","color":"#2886bc","outline":"none","borderRadius":"8px","background":"#fff","width":"auto","lineHeight":"44px","fontSize":"16px","height":"48px"}'  type="primary" @click="onSubmit">提交</el-button>
        <el-button :style='{"border":"2px solid #ccc","cursor":"pointer","padding":"0 35px","boxShadow":"inset 0px 0px 12px 0px #eee","margin":"010px","color":"#999","outline":"none","borderRadius":"8px","background":"#fff","width":"auto","lineHeight":"44px","fontSize":"16px","height":"48px"}' @click="back()">返回</el-button>
      </el-form-item>
    </el-form>
</div>
</template>

<script>
  export default {
    data() {
      return {
        id: '',
        baseUrl: '',
        ro:{
            dingdanbianhao : false,
            shangpinmingcheng : false,
            shangpinjiage : false,
            shangpinzhongliang : false,
            chaozhongfeiyong : false,
            dingjinbili : false,
            dingjin : false,
            shouhuodizhi : false,
            gouwudizhi : false,
            gouwushuoming : false,
            zuiwanwanchengshijian : false,
            dingdanshijian : false,
            xuehao : false,
            daigouzhanghao : false,
            sfsh : false,
            shhf : false,
            ispay : false,
        },
        type: '',
        userTableName: localStorage.getItem('UserTableName'),
        ruleForm: {
          dingdanbianhao: this.getUUID(),
          shangpinmingcheng: '',
          shangpinjiage: '',
          shangpinzhongliang: '',
          chaozhongfeiyong: '',
          dingjinbili: '',
          dingjin: '',
          shouhuodizhi: '',
          gouwudizhi: '',
          gouwushuoming: '',
          zuiwanwanchengshijian: '',
          dingdanshijian: '',
          xuehao: '',
          daigouzhanghao: '',
          ispay: '',
        },
        dingjinbiliOptions: [],


        rules: {
          dingdanbianhao: [
          ],
          shangpinmingcheng: [
          ],
          shangpinjiage: [
            { validator: this.$validate.isNumber, trigger: 'blur' },
          ],
          shangpinzhongliang: [
            { validator: this.$validate.isNumber, trigger: 'blur' },
          ],
          chaozhongfeiyong: [
            { validator: this.$validate.isNumber, trigger: 'blur' },
          ],
          dingjinbili: [
            { validator: this.$validate.isNumber, trigger: 'blur' },
          ],
          dingjin: [
            { validator: this.$validate.isNumber, trigger: 'blur' },
          ],
          shouhuodizhi: [
          ],
          gouwudizhi: [
          ],
          gouwushuoming: [
          ],
          zuiwanwanchengshijian: [
          ],
          dingdanshijian: [
          ],
          xuehao: [
          ],
          daigouzhanghao: [
          ],
          sfsh: [
          ],
          shhf: [
          ],
          ispay: [
          ],
        },
      };
    },
    computed: {


        dingjin:{
            get: function () {
                return 1*this.ruleForm.shangpinjiage*this.ruleForm.dingjinbili
            }
        },

    },
    components: {
    },
    created() {
	  //this.bg();
      let type = this.$route.query.type ? this.$route.query.type : '';
      this.init(type);
      this.baseUrl = this.$config.baseUrl;
      this.ruleForm.dingdanshijian = this.getCurDateTime()
    },
    methods: {
      getMakeZero(s) {
          return s < 10 ? '0' + s : s;
      },
      // 下载
      download(file){
        window.open(`${file}`)
      },
      // 初始化
      init(type) {
        this.type = type;
        if(type=='cross'){
          var obj = JSON.parse(localStorage.getItem('crossObj'));
          for (var o in obj){
            if(o=='dingdanbianhao'){
              this.ruleForm.dingdanbianhao = obj[o];
              this.ro.dingdanbianhao = true;
              continue;
            }
            if(o=='shangpinmingcheng'){
              this.ruleForm.shangpinmingcheng = obj[o];
              this.ro.shangpinmingcheng = true;
              continue;
            }
            if(o=='shangpinjiage'){
              this.ruleForm.shangpinjiage = obj[o];
              this.ro.shangpinjiage = true;
              continue;
            }
            if(o=='shangpinzhongliang'){
              this.ruleForm.shangpinzhongliang = obj[o];
              this.ro.shangpinzhongliang = true;
              continue;
            }
            if(o=='chaozhongfeiyong'){
              this.ruleForm.chaozhongfeiyong = obj[o];
              this.ro.chaozhongfeiyong = true;
              continue;
            }
            if(o=='dingjinbili'){
              this.ruleForm.dingjinbili = obj[o];
              this.ro.dingjinbili = true;
              continue;
            }
            if(o=='dingjin'){
              this.ruleForm.dingjin = obj[o];
              this.ro.dingjin = true;
              continue;
            }
            if(o=='shouhuodizhi'){
              this.ruleForm.shouhuodizhi = obj[o];
              this.ro.shouhuodizhi = true;
              continue;
            }
            if(o=='gouwudizhi'){
              this.ruleForm.gouwudizhi = obj[o];
              this.ro.gouwudizhi = true;
              continue;
            }
            if(o=='gouwushuoming'){
              this.ruleForm.gouwushuoming = obj[o];
              this.ro.gouwushuoming = true;
              continue;
            }
            if(o=='zuiwanwanchengshijian'){
              this.ruleForm.zuiwanwanchengshijian = obj[o];
              this.ro.zuiwanwanchengshijian = true;
              continue;
            }
            if(o=='dingdanshijian'){
              this.ruleForm.dingdanshijian = obj[o];
              this.ro.dingdanshijian = true;
              continue;
            }
            if(o=='xuehao'){
              this.ruleForm.xuehao = obj[o];
              this.ro.xuehao = true;
              continue;
            }
            if(o=='daigouzhanghao'){
              this.ruleForm.daigouzhanghao = obj[o];
              this.ro.daigouzhanghao = true;
              continue;
            }
          }
        }
        // 获取用户信息
        this.$http.get(this.userTableName + '/session', {emulateJSON: true}).then(res => {
          if (res.data.code == 0) {
            var json = res.data.data;
            if((json.xuehao!=''&&json.xuehao) || json.xuehao==0){
                this.ruleForm.daigouzhanghao = json.xuehao
            }
          }
        });
        this.dingjinbiliOptions = "0.5".split(',')
      },

    // 多级联动参数
      // 多级联动参数
      info(id) {
        this.$http.get('daigoudingdan/detail/${id}', {emulateJSON: true}).then(res => {
          if (res.data.code == 0) {
            this.ruleForm = res.data.data;
          }
        });
      },
      // 提交
      onSubmit() {

          this.ruleForm.dingjin = this.dingjin
        //更新跨表属性
        var crossuserid;
        var crossrefid;
        var crossoptnum;
        this.$refs["ruleForm"].validate(valid => {
          if(valid) {
            if(this.type=='cross'){
                 var statusColumnName = localStorage.getItem('statusColumnName');
                 var statusColumnValue = localStorage.getItem('statusColumnValue');
                 if(statusColumnName && statusColumnName!='') {
                     var obj = JSON.parse(localStorage.getItem('crossObj'));
                     if(!statusColumnName.startsWith("[")) {
                         for (var o in obj){
                             if(o==statusColumnName){
                                 obj[o] = statusColumnValue;
                             }
                         }
                         var table = localStorage.getItem('crossTable');
                         this.$http.post(table+'/update', obj).then(res => {});
                     } else {
                            crossuserid=Number(localStorage.getItem('userid'));
                            crossrefid=obj['id'];
                            crossoptnum=localStorage.getItem('statusColumnName');
                            crossoptnum=crossoptnum.replace(/\[/,"").replace(/\]/,"");
                     }
                 }
            }
            if(crossrefid && crossuserid) {
                 this.ruleForm.crossuserid=crossuserid;
                 this.ruleForm.crossrefid=crossrefid;
                 var params = {
                     page: 1,
                     limit: 10,
                     crossuserid:crossuserid,
                     crossrefid:crossrefid,
                 }
                 this.$http.get('daigoudingdan/list', {
                  params: params
                 }).then(res => {
                     if(res.data.data.total>=crossoptnum) {
                         this.$message({
                          message: localStorage.getItem('tips'),
                          type: 'success',
                          duration: 1500,
                         });
                          return false;
                     } else {
                         // 跨表计算


                          this.$http.post('daigoudingdan/add', this.ruleForm).then(res => {
                              if (res.data.code == 0) {
                                  this.$message({
                                      message: '操作成功',
                                      type: 'success',
                                      duration: 1500,
                                      onClose: () => {
                                          this.$router.go(-1);
                                      }
                                  });
                              } else {
                                  this.$message({
                                      message: res.data.msg,
                                      type: 'error',
                                      duration: 1500
                                  });
                              }
                          });
                     }
                 });
             } else {


                  this.$http.post('daigoudingdan/add', this.ruleForm).then(res => {
                     if (res.data.code == 0) {
                          this.$message({
                              message: '操作成功',
                              type: 'success',
                              duration: 1500,
                              onClose: () => {
                                  this.$router.go(-1);
                              }
                          });
                      } else {
                          this.$message({
                              message: res.data.msg,
                              type: 'error',
                              duration: 1500
                          });
                      }
                  });
             }
          }
        });
      },
      // 获取uuid
      getUUID () {
        return new Date().getTime();
      },
      // 返回
      back() {
        this.$router.go(-1);
      },
    }
  };
</script>

<style rel="stylesheet/scss" lang="scss" scoped>
	.el-date-editor.el-input {
		width: auto;
	}
	
	.add-update-preview .el-form-item /deep/ .el-form-item__label {
	  padding: 0 15px;
	  color: #2886bc;
	  font-weight: 500;
	  width: 150px;
	  font-size: 16px;
	  line-height: 40px;
	  text-align: right;
	}
	
	.add-update-preview .el-form-item /deep/ .el-form-item__content {
	  margin-left: 150px;
	}
	
	.add-update-preview .el-input /deep/ .el-input__inner {
	  border: 1px solid #ddd;
	  border-radius: 30px;
	  padding: 0 10px;
	  box-shadow: 0 0 0px rgba(64, 158, 255, .5);
	  outline: none;
	  color: #666;
	  width: 350px;
	  font-size: 14px;
	  height: 40px;
	}
	
	.add-update-preview .el-select /deep/ .el-input__inner {
	  border: 1px solid #ddd;
	  border-radius: 30px;
	  padding: 0 10px;
	  box-shadow: 0 0 0px rgba(64, 158, 255, .5);
	  outline: none;
	  color: #666;
	  width: 350px;
	  font-size: 14px;
	  height: 40px;
	}
	
	.add-update-preview .el-date-editor /deep/ .el-input__inner {
	  border: 1px solid #ddd;
	  border-radius: 30px;
	  padding: 0 10px 0 30px;
	  box-shadow: 0 0 0px rgba(64, 158, 255, .5);
	  outline: none;
	  color: #2886bc;
	  width: 350px;
	  font-size: 14px;
	  height: 40px;
	}
	
	.add-update-preview /deep/ .el-upload--picture-card {
		background: transparent;
		border: 0;
		border-radius: 0;
		width: auto;
		height: auto;
		line-height: initial;
		vertical-align: middle;
	}
	
	.add-update-preview /deep/ .upload .upload-img {
	  border: 1px dashed #666;
	  cursor: pointer;
	  border-radius: 6px;
	  color: #666;
	  width: 100px;
	  font-size: 32px;
	  line-height: 100px;
	  text-align: center;
	  height: 100px;
	}
	
	.add-update-preview /deep/ .el-upload-list .el-upload-list__item {
	  border: 1px dashed #666;
	  cursor: pointer;
	  border-radius: 6px;
	  color: #666;
	  width: 100px;
	  font-size: 32px;
	  line-height: 100px;
	  text-align: center;
	  height: 100px;
	}
	
	.add-update-preview /deep/ .el-upload .el-icon-plus {
	  border: 1px dashed #666;
	  cursor: pointer;
	  border-radius: 6px;
	  color: #666;
	  width: 100px;
	  font-size: 32px;
	  line-height: 100px;
	  text-align: center;
	  height: 100px;
	}
	
	.add-update-preview .el-textarea /deep/ .el-textarea__inner {
	  border: 1px solid #ddd;
	  border-radius: 30px;
	  padding: 12px;
	  box-shadow: 0 0 0px rgba(64, 158, 255, .5);
	  outline: none;
	  color: #666;
	  width: 350px;
	  font-size: 14px;
	  height: 120px;
	}
</style>
